﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CMP.ERP.BLL;
using System.Data;
using CMP.ERP.Model;

namespace CMP.ERP.Admin.Controllers
{
    public class calendarController : Controller
    {
        //
        // GET: /calendar/
        WareHoseBLL bll = new WareHoseBLL();
        //显示仓库信息
        public ActionResult calendar_Show()
        {
            List<WareHose> list = new List<WareHose>();
            DataTable data = bll.WareHoseShow();
            foreach (DataRow item in data.Rows)
            {
                WareHose e = new WareHose();
                e.WareHose_Id = Convert.ToInt32(item[0]);
                e.WareHose_Name = item[1].ToString();
                e.WareHose_Address = item[2].ToString();
                e.WareHose_People = item[3].ToString();
                e.WareHose_Phone = item[4].ToString();
                e.WareHose_Remark = item[5].ToString();
                e.Commodity_Url = item[6].ToString();
                e.Num = Convert.ToInt32(item[7]);
                list.Add(e);
            }
            return View(list);
        }
        //弹出添加仓库信息视图  
        public ActionResult calendar_Add()
        {
            return View();
        }
        //实现添加仓库信息功能
        [HttpPost]
        public ActionResult calendar_Add(WareHose e, HttpPostedFileBase images)
        {
            if (images != null && images.ContentLength > 0)
            {
                string image = e.WareHose_Name + images.FileName.Substring(images.FileName.LastIndexOf('.'));//对文件名进行截取，截取方式以.进行截取，保留.后的字符
                images.SaveAs(Server.MapPath("/NewFolder1/" + image));
                e.Commodity_Url = "/NewFolder1/" + image;

            }
            if (bll.AddWareHose(e) > 0)
            {
                return Content("<script>alert('添加成功!');location.href='/calendar/calendar_Show'</script>");
            }
            else
            {
                return Content("<script>alert('添加失败!');location.href='/calendar/calendar_Add'</script>");
            }
        }
        //删除仓库信息
        public ActionResult calendar_Del(int id)
        {
            if (bll.DelWareHose(id) > 0)
            {
                return Content("<script>alert('删除成功!');location.href='/calendar/calendar_Show'</script>");
            }
            else
            {
                return Content("<script>alert('删除失败!');location.href='/calendar/calendar_Show'</script>");
            }
        }
        //修改仓库信息 
        public ActionResult calendar_Upd(int id)
        {
            WareHose e = new WareHose();
            DataTable data = bll.GetWareHose(id);
            foreach (DataRow item in data.Rows)
            {
                e.WareHose_Id = Convert.ToInt32(item[0]);
                e.WareHose_Name = item[1].ToString();
                e.WareHose_Address = item[2].ToString();
                e.WareHose_People = item[3].ToString();
                e.WareHose_Phone = item[4].ToString();
                e.WareHose_Remark = item[5].ToString();
                e.Commodity_Url = item[6].ToString();
            }
            return View(e);
        }
        //实现修改仓库信息功能
        [HttpPost]
        public ActionResult calendar_Upd(WareHose s, HttpPostedFileBase images)
        {
            if (images != null && images.ContentLength > 0)
            {
                string image = s.WareHose_Name + images.FileName.Substring(images.FileName.LastIndexOf('.'));//对文件名进行截取，截取方式以.进行截取，保留.后的字符
                images.SaveAs(Server.MapPath("/NewFolder1/" + image));
                s.Commodity_Url = "/NewFolder1/" + image;

            }
            if (bll.UpdWareHose(s) > 0)
            {
                return Content("<script>alert('修改成功!');location.href='/calendar/calendar_Show'</script>");
            }
            else
            {
                return Content("<script>alert('修改失败!');location.href='/calendar/calendar_Upd'</script>");
            }
        }
        //根据仓库名称查询仓库信息
        public ActionResult calendar_Get(string WareHose_Name)
        {
            List<WareHose> list = new List<WareHose>();
            DataTable data = bll.SelWareHose(WareHose_Name);
            foreach (DataRow item in data.Rows)
            {
                WareHose e = new WareHose();
                e.WareHose_Id = Convert.ToInt32(item[0]);
                e.WareHose_Name = item[1].ToString();
                e.WareHose_Address = item[2].ToString();
                e.WareHose_People = item[3].ToString();
                e.WareHose_Phone = item[4].ToString();
                e.WareHose_Remark = item[5].ToString();
                e.Commodity_Url = item[6].ToString();
                list.Add(e);
            }
            return View("calendar_Show", list);
        }
    }
}
